package com.juyu.miliao.api.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 新人主播mq
 */
@Component
@Slf4j
public class NewHostUserMq {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 新增主播mq消息
     * @param message
     * {
     *   "userId"：1001  //用户id
     *   "createTime"：1499825149257 //毫秒时间戳
     * }
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue("userAuthenticationIn:success:queue"),
            exchange = @Exchange("userAuthenticationIn:success:exchange"),
            key = "userAuthenticationIn:success:routingKey"))
    public void setNewHostUser(String message) {
        log.info("新增主播mq消息:message-----"+message);
        if (!StringUtils.isEmpty(message)) {
            JSONObject jsStr = JSONObject.parseObject(message);
            String userId = jsStr.get("userId").toString();
            String createTime = jsStr.get("createTime").toString();
            log.info("userId：：："+userId);
            log.info("createTime：：："+createTime);
            stringRedisTemplate.opsForZSet().add("thirtyDaysNewHost",userId+"",Long.parseLong(createTime));
        }
    }
}
